package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.userInfo.entity.TodayRecord;

import java.util.List;

public class TodayAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;

    private List<TodayRecord.ResponseBean> todayRecords = null;

    public TodayAdapter(Context context) {
        this.mContext = context;
    }

    public void setTodayRecords(List<TodayRecord.ResponseBean> list) {
        this.todayRecords = list;
    }

    public void addTodayRecords(List<TodayRecord.ResponseBean> list) {
        this.todayRecords.addAll(list);
    }

    public List<TodayRecord.ResponseBean> getTodayRecords() {
        return this.todayRecords;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_today_item_view, parent, false);
        return new TodayViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final TodayRecord.ResponseBean responseBean = this.todayRecords.get(position);
        TodayViewHolder viewHolder = (TodayViewHolder) holder;
        viewHolder.txt_article_title.setText(responseBean.getTitle());
        viewHolder.txt_article_time.setText(DateUtils.ConvertDate(responseBean.getAdd_time()));

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送的实体类
                ArticleDetaisRequest request = new ArticleDetaisRequest();
                request.setContribute_id(responseBean.getObj_id());
                request.setType(5);
                request.setTopic_id(0);
                Intent intent = new Intent(mContext, SingleArticleDetailsActivity.class);
                intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, request);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (todayRecords == null) {
            return 0;
        }
        return todayRecords.size();
    }


    public static class TodayViewHolder extends RecyclerView.ViewHolder {

        private TextView txt_article_title, txt_article_time;

        public TodayViewHolder(View itemView) {
            super(itemView);
            txt_article_title = itemView.findViewById(R.id.txt_article_title);
            txt_article_time = itemView.findViewById(R.id.txt_article_time);
        }
    }
}
